package com.nbjtjc.safe.mapper.danger;

import com.nbjtjc.safe.model.danger.DangerInspectPublic;
import com.nbjtjc.util.MyMapper;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface DangerInspectPublicMapper extends MyMapper<DangerInspectPublic> {
    List<DangerInspectPublic> search(@Param("dip") DangerInspectPublic dangerInspectPublic
            , @Param("ids") List<Integer> validatorIds);

    List<DangerInspectPublic> searchSimple(@Param("dip") DangerInspectPublic dangerInspectPublic
            , @Param("ids") List<Integer> validatorIds);

    @ResultMap("detailInspect")
    @Select(" SELECT danger_inspect_public.*,sys_dept.name AS targetDeptName " +
            "    ,sys_user.username AS leaderUserName " +
            "    FROM danger_inspect_public " +
            "    LEFT JOIN sys_dept ON sys_dept.id=danger_inspect_public.target_dept_id " +
            "    LEFT JOIN sys_user ON sys_user.user_id=danger_inspect_public.leader_user_id " +
            " where danger_inspect_public.id=#{dip.id}")
    DangerInspectPublic getDetail(@Param("dip") DangerInspectPublic dangerInspectPublic);

    @Update("update danger_inspect_public set status=#{status} where id=#{inspectPublicId}")
    int updateInspectStatus(@Param("status") int status, @Param("inspectPublicId") int inspectPublicId);

    @Update("update danger_inspect_public set status=#{status} , finish=1 where id=#{inspectPublicId}")
    int finish(@Param("status") int status, @Param("inspectPublicId") int inspectPublicId);

    @Select("<script>" +
            "select danger_inspect_public.id from" +
            " danger_inspect_public where target_dept_id in " +
            " <foreach item='deptId' index='index' collection='deptIds'      open='(' separator=',' close=')'> " +
            "#{deptId}" +
            " </foreach> " +
            " and create_time <![CDATA[ >= ]]> #{startDate} and create_time <![CDATA[ <= ]]> #{endDate}"
            + "</script>")
    List<Integer> findDangerInspectIdsByDeptId(@Param("deptIds") List<Integer> deptIds, @Param("startDate") Date startDate
            , @Param("endDate") Date endDate);

    @Select("<script>" +
            "select danger_inspect_public.id,danger_inspect_public.target_dept_id,sys_dept.name as targetDeptName from danger_inspect_public" +
            " left join sys_dept on sys_dept.id= danger_inspect_public.target_dept_id" +
            "  where target_dept_id in " +
            " <foreach item='deptId' index='index' collection='deptIds'      open='(' separator=',' close=')'> " +
            "#{deptId}" +
            " </foreach> " +
            " and create_time <![CDATA[ >= ]]> #{startDate} and create_time <![CDATA[ <= ]]> #{endDate}"
            + "</script>")
    List<DangerInspectPublic> findDangerInspectByDeptId(@Param("deptIds") List<Integer> deptIds, @Param("startDate") Date startDate
            , @Param("endDate") Date endDate);

    @Select("<script>" +
            "select danger_inspect_public.id,danger_inspect_public.target_dept_id," +
            "danger_inspect_public.finish ,sys_dept.name AS targetDeptName from" +
            " danger_inspect_public " +
            " LEFT JOIN sys_dept ON sys_dept.id=danger_inspect_public.target_dept_id" +
            " where target_dept_id in " +
            " <foreach item='deptId' index='index' collection='deptIds'      open='(' separator=',' close=')'> " +
            "#{deptId}" +
            " </foreach> " +
            " and create_time <![CDATA[ >= ]]> #{startDate} and create_time <![CDATA[ <= ]]> #{endDate}"
            + "</script>")
    List<DangerInspectPublic> findInspectPublicByDeptIds(@Param("deptIds") List<Integer> deptIds, @Param("startDate") Date startDate
            , @Param("endDate") Date endDate);

    @Select("<script>" +
            "select danger_inspect_public.id,danger_inspect_public.target_dept_id," +
            "danger_inspect_public.finish, process_instance_id,sys_dept.name AS targetDeptName from" +
            " danger_inspect_public " +
            " LEFT JOIN sys_dept ON sys_dept.id=danger_inspect_public.target_dept_id" +
            " where finish=0  and  target_dept_id in " +
            " <foreach item='deptId' index='index' collection='deptIds'      open='(' separator=',' close=')'> " +
            "#{deptId}" +
            " </foreach>  order by danger_inspect_public.id desc" +
            "</script>")
    List<DangerInspectPublic> findInspectPublicByDeptIdsUnFinish(@Param("deptIds") List<Integer> deptIds);

    @Delete("delete from danger_inspect_public where inspect_task_id =#{taskId}")
    int deleteByTaskId(@Param("taskId") int taskId);
}